<?php
session_start();
include 'db.php';
include 'userclass.php';

/*
Author: Hallvard Westman
Project: Links
 ---------------------------------REGISTER USER----------------------------------------*/
//redirect if user is logged in
if ($user->loggedOn()){
header('Location:links.php?id='.$user->getID());
exit;	
}

//Inserting from register form into db
if (isset ($_POST['UserId'])) {
	
	if (($_POST['UserId'])!='Username'){
	//Encrypts password with md5
	$Word=substr(md5($_POST['Word']),0,32);

  	$sql = 'INSERT INTO li_users (UserId,Word)
			VALUES(:UserId, :Word)';
	
	$sth = $db->prepare($sql);
	$sth->bindParam(':UserId',$_POST['UserId']);
	$sth->bindParam(':Word',$Word);
	$res = $sth->execute();
	
	
	
	
	if(!$res){
	echo "<a>Username allready taken</a>";
	}
	else{
	$user->dologin($_POST['UserId'], $Word);
	header('location: links.php?id='.$_POST['UserId']);
	}
	}
	else
		echo "nono, you must write your own username";
	
}	

include 'header.php';
include 'bar.php';

/*---------------------------------FORM FOR REGISTERING----------------------------------------*/
?>

<form method="post" action="" onsubmit="return validateform();" enctype="multipart/form-data">
<ul>

<li><input type="text" name="UserId" value="Username"/></li>

<li><input type="password" name="Word" value="Word"/></li>
<li><button type='submit'/>Register</button></li>
</ul>

</form>

<?php include 'footer.php';?>

